

//
//  SearchShopTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/25.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "SearchShopTabVC.h"
#import "SearchShopCell.h"
#import "ShopSubModel.h"
@interface SearchShopTabVC ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic,assign) NSInteger pageNum;
@property(nonatomic,assign) NSInteger pageSize;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@end
@implementation SearchShopTabVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNum = 1;
    self.pageSize = 10;
    self.manager = [AFHTTPSessionManager manager];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}
- (void)setShopMuta:(NSMutableArray *)shopMuta
{
    _shopMuta = shopMuta;
    if (!shopMuta.count) {
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    }else {
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
        self.tableView.mj_footer = footer;
        [footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"]
 forState:MJRefreshStateNoMoreData];
        footer.stateLabel.font = [UIFont systemFontOfSize:12];
        footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        if (shopMuta.count< 10) {
            footer.state = MJRefreshStateNoMoreData;
        }
    }
    [self.tableView reloadData];
}
-(void)reloadMore
{
    self.pageNum ++;
    self.urlString = [NSString stringWithFormat:@"%@/shop/selectSerch",kUrl];
    self.dict = @{
                  @"content":self.text,
                  @"pageNumber":[NumberSwitchString numberSwitchString:self.pageNum],
                  @"pageSize":[NumberSwitchString numberSwitchString:self.pageSize],
                  @"uid":[UserDefaultsHelper readUid],
                  @"token":[UserDefaultsHelper readToken],
                  };
    [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        XLog(@"SearchShopTabVC:%@",responseObject);
        NSArray *temp = [responseObject objectForKey:@"date"];
        if (temp.count) {
            for (NSDictionary *dict in temp) {
                ShopSubModel *model =[ShopSubModel mj_objectWithKeyValues:dict];
                [self.shopMuta addObject:model];
            }
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        }else {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XLog(@"error:%@",error);
    }];
    
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];

    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.shopMuta.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    SearchShopCell *cell = [SearchShopCell cellNibWithTableView:tableView];
    ShopSubModel *model = self.shopMuta[indexPath.row];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.returnValueBlock) {
        self.returnValueBlock(indexPath.row);
    };
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}
@end
